﻿Imports System.Data.OleDb
Public Class addNewDish
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btn_enter_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_enter.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_foodImage.FileName)
        If fup_foodImage.HasFile Then
            fup_foodImage.SaveAs(Server.MapPath("picture") & "/" & newFileName)
        End If

        Dim sql As String = "INSERT INTO JamaicanKitchen (Foodname,FoodImage,Ingredients,DirectionToPrepare,ServingSuggestion,AverageCookingTime,RatingStars,AddedBy)VALUES (@j1,@j2,@j3,@j4,@j5,@j6,@j7,@j8)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("JamaicanConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@j1", tb_foodName.Text)
        cmd.Parameters.AddWithValue("@j2", "picture/" & newFileName)
        cmd.Parameters.AddWithValue("@j3", tb_Ingredients.Text)
        cmd.Parameters.AddWithValue("@j4", tb_directionToPrepare.Text)
        cmd.Parameters.AddWithValue("@j5", tb_ServingSuggestion.Text)
        cmd.Parameters.AddWithValue("@j6", tb_AverageCookingTime.Text)
        cmd.Parameters.AddWithValue("@j7", rbl_ratingStars.SelectedValue)
        cmd.Parameters.AddWithValue("@j8", User.Identity.Name)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("list.aspx")
    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function
End Class